<template>
  <div class="hm-upload">
    <!-- 基本图片上传
        el-upload
            action:上传的接口地址，必传参数
            name:上传文件对应的字段名
            show-file-list:是否显示文件列表
            before-upload:上传前的回调，限制 上传文件大小与类型
            on-success:上传成功回调，拿到接口返回值
            http-request:自定义上传 (on-success无效了)

          删除功能
            1：布局 按钮
            2：点击删除图片

          图片预览
            1：布局
            2：准备一个弹框，用于显示大图片
            3：点击放大按钮显示弹框

          进度条
            1：布局
            2：绑定进度值
            3：在上传过程中才显示进度条
            4：上传过程中要更新进度值
            5：上传完成不要再显示进度条
     -->

    <el-upload
      class="avatar-uploader"
      action="#"
      :show-file-list="false"
      :before-upload="beforeUpload"
      :http-request="httpRequest"
    >
      <img v-if="value" v-imgerror :src="value" class="avatar">
      <i v-else class="el-icon-plus avatar-uploader-icon" />
    </el-upload>
    <!-- v-if/v-show:区别
        v-if场景：用于切换较少的场景，且条件较多的时候
        v-show场景：用于切换频繁，且条件较少
     -->
    <!-- 删除按钮 -->
    <i v-if="value" class="el-icon-delete del" @click="delEvent" />
    <!-- 放大按钮 -->
    <i v-if="value" class="el-icon-zoom-in big" @click="showBigImg = true" />
    <!-- 放大图片预览 -->
    <el-dialog title="图片预览" :visible.sync="showBigImg" width="700px">
      <div style="text-align: center">
        <img v-imgerror style="width: 100%" :src="value" alt="">
      </div>
    </el-dialog>
    <!-- 进度条 -->
    <el-progress v-if="loading" :percentage="percentage" />
  </div>
</template>
<script>
import COS from 'cos-js-sdk-v5'
export default {
  name: 'HmUploadImg',
  props: {
    value: { type: String, default: '' }
  },
  data() {
    return {
      showBigImg: false,
      imageUrl: '',
      cos: '',
      percentage: 0,
      loading: false
    }
  },
  created() {
    this.cos = new COS({
      SecretId: 'AKIDpg3Qi3FE1byEJBtNffWClqiT6bwFNVVM',
      SecretKey: 'tkOm196zjH0bCVjpofKmzzqW1JRcaMwL'
    })
  },
  methods: {
    beforeUpload(file) {
      // size  ,type   ,name
      const limitType =
        file.type === 'image/png' ||
        file.type === 'image/jpeg' ||
        file.type === 'image/gif'
      if (!limitType) {
        this.$message.error('请上传png,jpg,gif格式图片')
      }
      const limitSize = file.size / 1024 / 1024 < 100
      if (!limitSize) {
        this.$message.error('请上传100Mb内的图片')
      }
      // console.log(file)
      return limitType && limitSize
    },
    httpRequest(res) {
      // console.log(res.file)
      this.loading = true
      this.$emit('input', '')
      this.cos.putObject(
        {
          Bucket: 'hr83-1312526868' /* 必须:存储桶编号 */,
          Region:
            'ap-hongkong' /* 存储桶所在地域，必须字段 ，选择的服务器编号 */,
          Key: '/hm89/' + res.file.name /* 必须:文件名 */,
          StorageClass: 'STANDARD',
          Body: res.file, // 上传文件对象
          onProgress: (progressData) => {
            // 进度值
            this.percentage = +(progressData.percent * 100).toFixed(2)
            if (progressData.percent === 1) {
              this.loading = false
            }
            // console.log(JSON.stringify(progressData))
          }
        },
        (err, data) => {
          // 成功或者失败返回值
          // console.log(err || data)
          if (!err) {
            // this.imageUrl = 'http://' + data.Location
            this.$emit('input', 'http://' + data.Location)
          }
        }
      )
    },
    // 删除点击事件
    delEvent() {
      this.$emit('input', '')
    }
  }
}
</script>
<style lang="scss" scoped>
// scoped:看得到的可以管到，看不到管不到
.avatar-uploader ::v-deep .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader ::v-deep .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

.hm-upload {
  position: relative;
  width: 178px;
  &:hover {
    .del,
    .big {
      display: block;
    }
  }
  .del,
  .big {
    position: absolute;
    top: 5px;
    right: 5px;
    font-size: 26px;
    color: red;
    cursor: pointer;
    display: none;
  }
  .big {
    right: 35px;
  }
}
</style>
